package com.work.bookstore.controller;

import com.work.bookstore.mapper.BookMapper;
import com.work.bookstore.mapper.OrderMapper;
import com.work.bookstore.mapper.ShoppingCarMapper;
import com.work.bookstore.pojo.Order;
import com.work.bookstore.pojo.ShoppingCar;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ppg007
 * @date 2021/1/1 18:01
 */
@Controller
public class OrderController {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private ShoppingCarMapper shoppingCarMapper;

    @RequestMapping(value = "/createOrder",method = RequestMethod.POST)
    @ResponseBody
    public String createOrder(HttpServletRequest request){
        String userAccount=request.getParameter("userAccount");
        String bookList=request.getParameter("bookList");
        String status=request.getParameter("status");
        String position=request.getParameter("position");
        String[] temp=bookList.split("∫");
        List<String> bought=new ArrayList<>();
        double sum=0;
        for (String str:temp){
            String price=orderMapper.queryPriceById(str);
            bought.add(str);
            String[] p=price.split("¥");
            sum+=Double.parseDouble(p[1]);
        }
        if ("shoppingCar".equals(position)){
            orderMapper.addOrder(userAccount,bookList,status,sum);
            ShoppingCar shoppingCar=shoppingCarMapper.queryShoppingCarByAccount(userAccount);
            String[] old=shoppingCar.getBookList().split("∫");
            StringBuilder after=new StringBuilder();
            for (String t:old){
                if (bought.contains(t)){
                    bought.remove(t);
                }
                else {
                    after.append(t).append("∫");
                }
            }
            shoppingCarMapper.updateShoppingCar(after.toString(),userAccount);
            return refresh(userAccount);
        }
        else {
            orderMapper.addOrder(userAccount,bookList,status,sum);
            return Double.toString(sum);
        }
    }

    @RequestMapping(value = "/updateOrder",method = RequestMethod.POST)
    @ResponseBody
    public String updateOrder(HttpServletRequest request){
        String status=request.getParameter("status");

        return "success";
    }

    @RequestMapping(value = "/getOrder",method = RequestMethod.POST)
    @ResponseBody
    public String getOrder(HttpServletRequest request){
        String status=request.getParameter("status");
        String userAccount=request.getParameter("userAccount");
        List<Order> orders=orderMapper.queryOrderByStatus(status,userAccount);

        JSONArray jsonArray=new JSONArray();
        for (Order order:orders){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id",order.getId());
            jsonObject.put("price",order.getPrice());
            jsonObject.put("bookList",order.getBookList());
            jsonArray.put(jsonObject);
        }
        return jsonArray.toString();
    }
    public String refresh(String account){
        ShoppingCar shoppingCar=shoppingCarMapper.queryShoppingCarByAccount(account);
        String bookList=shoppingCar.getBookList();
        String[] temp=bookList.split("∫");
        JSONArray jsonArray=new JSONArray();
        for (String t:temp){
            jsonArray.put(BookController.putData(bookMapper.queryBookByOrder(Integer.parseInt(t))));
        }
        return jsonArray.toString();
    }
}
